﻿namespace ECM7.NantContrib
{
	using System.IO;
	using System.Text;
	using NAnt.Core;
	using NAnt.Core.Attributes;

	/// <summary>
	/// Генерация файла App_Offline.htm
	/// </summary>
	[TaskName("app-offline")]
	public class AppOfflineTask : Task
	{
		/// <summary>
		/// Заголовок по умолчанию
		/// </summary>
		private const string DEFAULT_TITLE = "В данный момент идет техническое обслуживание сайта";

		/// <summary>
		/// Текст по умолчанию
		/// </summary>
		private const string DEFAULT_TEXT = "Попробуйте зайти позже";

		/// <summary>
		/// Имя файла
		/// </summary>
		private const string FILENAME = "App_Offline.htm";

		/// <summary>
		/// Путь для сохранения файла
		/// </summary>
		[TaskAttribute("path", Required = true)]
		public string Path { get; set; }

		/// <summary>
		/// Заголовок
		/// </summary>
		[TaskAttribute("title", Required = false)]
		public string Title { get; set; }

		/// <summary>
		/// Текст
		/// </summary>
		[TaskAttribute("text", Required = false)]
		public string Text { get; set; }

		/// <summary>
		/// Выполнение необходимых действий
		/// </summary>
		protected override void ExecuteTask()
		{
			string path = System.IO.Path.Combine(Path, FILENAME);

			using (TextWriter writer = new StreamWriter(path, false, Encoding.Unicode))
			{
				writer.WriteLine("<!DOCTYPE html PUBLIC ");
				writer.WriteLine("\"-//W3C//DTD XHTML 1.0 Transitional//EN\"");
				writer.WriteLine("\"http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd\" >");
				writer.WriteLine();

				writer.WriteLine("<html xmlns=\"http://www.w3.org/1999/xhtml\" >");
				writer.WriteLine("<head><title>Техническое обслуживание</title></head>");
				writer.WriteLine("<body>");
				writer.WriteLine();

				writer.Write("\t<h1>");
				writer.Write(Title ?? DEFAULT_TITLE);
				writer.WriteLine("</h1>");
				writer.WriteLine();

				writer.Write("\t<h2>");
				writer.Write(Text ?? DEFAULT_TEXT);
				writer.WriteLine("</h2>");
				writer.WriteLine();

				writer.WriteLine("\t<!--");
				writer.WriteLine("\tAdding additional hidden content so that IE Friendly Errors don't prevent");
				writer.WriteLine("\tthis message from displaying (note: it will show a \"friendly\" 404");
				writer.WriteLine("\terror if the content isn't of a certain size).");
				writer.WriteLine();

				for (int i = 0; i < 13; i++)
				{
					writer.WriteLine("\t<h2>Gone to Florida for the sun...</h2>");
				}

				writer.WriteLine("\t-->");
				writer.WriteLine();
				writer.WriteLine("</body>");
				writer.WriteLine("</html>");
			}
		}
	}
}
